// content-scripts/features/copyTextButton.js
window.copyTextButtonModule = (function() {
    // 依赖注入容器
    const dependencies = {
        extractRowData: null,
        formatRowData: null,
        showToast: null
    };

    // 模块初始化方法
    function init(config) {

        Object.keys(config).forEach(key => {
            if (key in dependencies) {
                dependencies[key] = config[key];
            }
        });
        console.log('依赖注入完成:', dependencies);
    }

    // 私有工具方法（降级实现）
    function fallbackShowToast(message, type) {
        console.log(`[${type}] ${message}`);
    }

    // 核心功能实现
    function createCopyTextButton(row) {
        const btn = document.createElement('button');
        btn.className = 'copy-btn copy-text-btn';
        btn.innerHTML = '📄';
        btn.title = '复制文本内容';
        btn.style.cssText = `
            background: none;
            border: none;
            cursor: pointer;
            font-size: 16px;
            padding: 4px;
            border-radius: 3px;
            background-color: rgba(0,0,0,0.05);
            transition: all 0.2s;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
        `;

        btn.addEventListener('click', async (e) => {
            const row = e.currentTarget.closest('[role="row"]');
            if (!row) {
                (dependencies.showToast || fallbackShowToast)('无法找到所在行', 'error');
                return;
            }

            try {
                // 使用注入的依赖
                const rowData = dependencies.extractRowData(row);
                const formattedText = dependencies.formatRowData(rowData);
                await navigator.clipboard.writeText(formattedText);

                // 使用注入的toast或降级方法
                (dependencies.showToast || fallbackShowToast)('文本复制成功', 'success');
            } catch (error) {
                console.error('复制失败:', error);
                (dependencies.showToast || fallbackShowToast)(`复制失败: ${error.message}`, 'error');
            }
        });

        return btn;
    }

    return {
        init,
        createCopyTextButton
    };
})();
